package net.aaa.hotel.controller;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import net.aaa.hotel.dao.T_sheshiDao;
import net.aaa.hotel.entity.T_TypeSheshi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author longlong
 * @Date 2020/11/24
 */
@RestController
@RequestMapping("sheshi")
public class SheShiController {
//    @Autowired
//    @Qualifier("fidsj")
    @Resource
    private T_sheshiDao sheShiDao;

    @RequestMapping("getAll")
    public Map getAll(Integer typeid){
        Map m=new HashMap();
        List<Map> allGdSheShi = sheShiDao.getAllGdSheShi();
        //根据类型查出这个类型的设施id
        List<Map> typeSheshi=sheShiDao.getTypeSheshi(typeid);
//        System.out.println("typeid+++"+typeid);
//        System.out.println("typeSheshi----"+typeSheshi);
        m.put("allSheshi",allGdSheShi);  // arrSheshi  代表所有不包含娱乐设施的设施
        m.put("ownsheshi",typeSheshi);    // 根据房间的id获取所有的设施id
        return m;

    }

/*
    */
/**
     * 删除房间类型有的设施
     * @param typeid
     * @return
     *//*


    @RequestMapping("deleteSheshi")
    public String deleteSheshi(Integer typeid){
        //先删除 后添加
        System.out.println("typeid+++++++"+typeid);
        Integer i=sheShiDao.deleteSheshi(typeid);
        if(i>0){
            return "1";
        }else {
            return "0";
        }
    }
*/



    /**
     * 添加房间类型的设施
     * @return
     */

    @RequestMapping("addTypeSheshi")
    public String addTypeSheshi(T_TypeSheshi typesheshi){
       // System.out.println(typesheshi);
        //先删除 后添加
        int i=sheShiDao.deleteSheshi(typesheshi);
        //添加这个类型的设施id
        int j=sheShiDao.addSheShi(typesheshi);
        if(j>0&&i>0){
            return "1";
        }
        return "0";
    }

    @RequestMapping("/upl")
    public Map getUploadPath(MultipartFile file) {
        String path = "";
        Map ma = new HashMap();

        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        UUID uuid = UUID.randomUUID();
        String objectName = uuid + suffix;
        try {
            // Endpoint以杭州为例，其它Region请按实际情况填写。
            //
            //oss-cn-beijing.aliyuncs.com
            String endpoint = "oss-cn-beijing.aliyuncs.com";
            // 云账号AccessKey有所有API访问权限，建议遵循阿里云安全最佳实践，创建并使用RAM子账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建。
            String accessKeyId = "LTAI4GCzjhtjNLVfjx8BPm1h";
            String accessKeySecret = "dmXsWFjrdNanypvoLXYuDsuO56slhP";
            String bucketName = "yuyongli";


            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 上传文件流。
            InputStream inputStream = file.getInputStream();
            PutObjectResult putObjectResult = ossClient.putObject(bucketName, objectName, inputStream);

            // 关闭OSSClient。
            ossClient.shutdown();

            path = "https://" + bucketName + "." + endpoint + "/" + objectName;
            //文件上传需要的参数  msg code data
            ma.put("code", 0);
            ma.put("msg", "");
            ma.put("data", path);


        } catch (IOException e) {
            e.printStackTrace();

        }

        return ma;
    }


}
 